package com.matt.repository.impl;

import com.matt.domain.ContractProduct;
import com.matt.foundation.vo.ContractProductVo;
import com.matt.repository.base.BaseNativeSqlRepository;
import com.matt.repository.base.ContractProductNativeRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by Ron
 * from com.matt
 * on 2018/6/29 0029.
 */
@Service
@Transactional
public class ContractProductNativeRepositoryImpl extends BaseNativeSqlRepository

        implements ContractProductNativeRepository {

    @Override
    public List<ContractProductVo> findByContractId(String contractId) {
        String sqlQuery=String.format("select cp.id as id,cp.created as created,cp.deleted as deleted,cp.productId as productId,cp.contractId as contractId,cp.clienteleId as clienteleId,cp.type as type,cp.typeVal as typeVal,cp.count as count,cp.remainCount as remainCount," +
                "cp.practicalAmount as practicalAmount,cp.practicalTotalAmount as practicalTotalAmount, cp.standardTotalAmount as standardTotalAmount, cp.remark as  remark,"+
                "pr.name as productName,pr.standardAmount as standardUnitAmount,pr.unit as productUnit from contract_product as cp left join product as pr on cp.productId=pr.id where true");
        sqlQuery +=String.format(" and cp.contractId='%s'",contractId);
        sqlQuery +=String .format(" order by cp.created desc");
        List<ContractProductVo> list= sqlObjectList(sqlQuery,ContractProductVo.class);
        return list;
    }

    @Override
    public List<ContractProduct> findReduceContractProducts(String clienteleId) {
        String sqlQuery=String.format("select cp.id as id ,cp.created as created,cp.deleted as deleted,cp.productId as productId,cp.contractId as contractId,cp.clienteleId as clienteleId,cp.productDetailType as productDetailType," +
                "cp.type as type,cp.typeVal as typeVal,cp.productType as productType,cp.count as count,cp.remainCount as remainCount,cp.practicalAmount as practicalAmount,cp.practicalTotalAmount as practicalTotalAmount,cp.standardAmount as standardAmount," +
                "cp.standardTotalAmount as standardTotalAmount,cp.remark as remark, " +
                "from contract_product as cp inner join contract as con on cp.contractId=con.id where cp.clienteleId and cp.productType=2 and con.status=0 and cp.remainCount>0");
        sqlQuery +=String.format(" order by cp.create desc");
        List<ContractProduct> list=sqlObjectList(sqlQuery,ContractProduct.class);
        return list;
    }
}
